# -*- coding: utf-8 -*-

# -------------------------------
# Author    : linlingwei
# Email     : 951035650@qq.com
# File Name : fNose.py
# Date      : 2019/9/16 
# IDE       : PyCharm
# Version   : 1.0.1
# -------------------------------
import traceback
import maya.cmds as mc
import maya.mel as mel

import configs.constants.facial_constant as fc
import configs.constants.global_constant as gc
import configs.constants.suffix_constant as sc
import configs.constants.nodeType_constant as ntc
import configs.constants.attribute_constant as ac

import scripts.commonCmds as con
reload(con)
import scripts.curveCmds as cr
import scripts.deformerCmds as dc
reload(dc)
import rig.facial.fCore as fCore
reload(fCore)

class Nose(fCore.FacialCore):
    def __init__(self):
        super(Nose,self).__init__(fc.nose)
        self.transform_init()
    
    def create(self):
        try:
            self.noseLoc()
            self.hide_doDelete()
        except:
            traceback.print_exc()
        return
    
    def noseLoc(self):
        
        tmp_nr = con.lname("tmp",fc.nose,fc.root)
        tmp_tip = con.lname("tmp",fc.noseTip)
        tmp_nal = con.lname(fc.lf,"tmp",fc.noseAlar)
        tmp_nar = con.lname(fc.rt,"tmp",fc.noseAlar)

        jr_y = mc.xform(tmp_nr, q=True, ws=True, t=True)
        j_y = mc.xform(tmp_tip, q=True, ws=True, t=True)

        dis = lambda a, b: pow(pow(a[0] - b[0], 2) + pow(a[1] - b[1], 2) + pow(a[2] - b[2], 2), .5)
        sv = dis(jr_y,j_y)*.1
        ctr_list = list()
        jot_list = list()
        
        for tn in [tmp_nr,tmp_tip,tmp_nal,tmp_nar]:
            ctr = cr.objToControl(tn,name=tn.replace("tmp_",""),colorId=21)
            cr.scaleXYZ(ctr,2)
            mc.parent(ctr,self.local_toParent)
            jot = mc.createNode(ntc.tJoint,name=con.lname(ctr.replace("_ctrl","_jot")))
            mc.parent(jot,ctr)
            con.channelAttrToDefault(jot)
            mc.setAttr(ctr+".s",sv,sv,sv)

            con.emptyGroup(ctr)
            con.emptyGroup(jot)

            mc.connectAttr(ctr+".t",jot+".t")
            mc.connectAttr(ctr+".r",jot+".r")
            mc.connectAttr(ctr+".s",jot+".s")
            
            ctr_list.append(ctr)
            jot_list.append(jot)
        
        con.changColor(ctr_list[0],13)
        con.changColor(ctr_list[1],17)

        con.localMirror(ctr_list[-1]+"_Zero")
        
        rotRoot = mc.createNode(ntc.tJoint,name = con.lname(fc.nose,fc.root,fc.rot,sc.sJot))
        mc.parent(rotRoot,jot_list[0],r=True)
        # con.channelAttrToDefault(rotRoot)
        mc.setAttr(rotRoot+".ty",-0.1,l=True)
        mc.connectAttr(ctr_list[0]+".r",rotRoot+".r")
        mc.disconnectAttr(ctr_list[0]+".r",jot_list[0]+".r")
        
        mc.parent(ctr_list[1]+"_Zero",ctr_list[2]+"_Zero",ctr_list[3]+"_Zero",ctr_list[0])
        
        mc.parent(jot_list[1]+"_Zero",rotRoot)
        mc.parent(jot_list[2]+"_Zero",jot_list[3]+"_Zero",jot_list[0])
        mc.parent(jot_list[0]+"_Zero",self.local_static)
        
        jot_list.append(rotRoot)
        mc.sets(jot_list,include=self.local_sets)
        return
if __name__ == '__main__':
    pass